package com.sphd.emanu.activity.materialsstorelist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.materials.MaterialsActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.materialsclassify.MaterialsclassifyFragment;
import com.sphd.emanu.fragment.materialslist.MaterialsListFragment;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.popupwindow.MorePopWindow;
import com.sphd.emanu.utils.PermissionConfig;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @Author Hu
 * @Time 2017/8/21 -- 18:24.
 * @Email 87683202@qq.com
 */
public class MaterialsStoreListActivity extends MVPBaseActivity<MaterialsStoreListContract.View, MaterialsStoreListPresenter> implements MaterialsStoreListContract.View, MorePopWindow.PopWinOnClickListener, Letter {
    MaterialsclassifyFragment classifyFragment;
    MaterialsListFragment listFragment;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    Intent intent;
    Bundle bundle;
    Category category;
    int level;
    @BindView(R.id.btn_more)
    TextView btnMore;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    private MorePopWindow morePopWindow;
    User user;
    List<Moudle> moudles;
    boolean isCanDo;
    String parentName = null;
    Category categoryParent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_materials_store_list);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        toolbar.setNavigationOnClickListener(v -> finish());
        morePopWindow.setPopWinOnClickListener(this);
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        level = bundle.getInt(Constant.LEVEL) + 1;
        category = (Category) bundle.getSerializable(Constant.CATEGORY);
        categoryParent = (Category) bundle.getSerializable(Constant.CATEGORY_PARENT);
        parentName = categoryParent.getName();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_STOREHOUSE_8);
        setTitleBar(toolbar, category.getName() + "---" + level);
        setRefresh(mySwf);
        //界面跳转过来 先进行权限或者是否是商品且是分类进行判断
        if (!isCanDo || ("商品".equals(parentName) && categoryParent.getIsLocked())) {
            btnMore.setVisibility(View.GONE);
        }
        //初始化更多按钮
        morePopWindow = new MorePopWindow(this);
        morePopWindow.matilr_stroage.setText("新增材料");

        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectNextCategory(category.getId());

    }

    private void setCurrent(int i, Category category) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        hideAll(ft);
        switch (i) {
            case 0:
                if (classifyFragment == null) {
                    classifyFragment = new MaterialsclassifyFragment();
                    bundle.putSerializable(Constant.LEVEL, level);
                    bundle.putSerializable(Constant.CATEGORY_LIST, (Serializable) category.getListCategory());
                    classifyFragment.setLetter(this);
                    classifyFragment.setArguments(bundle);
                    ft.add(R.id.my_fragment, classifyFragment);
                } else {
                    ft.show(classifyFragment);
                    classifyFragment.setData(category.getListCategory());
                }
                break;

            case 1:
                if (listFragment == null) {
                    listFragment = new MaterialsListFragment();
                    bundle.putSerializable(Constant.MATERIAL_LIST, (Serializable) category.getListMaterical());
                    listFragment.setArguments(bundle);
                    listFragment.setLetter(this);
                    ft.add(R.id.my_fragment, listFragment);
                } else {
                    ft.show(listFragment);
                    listFragment.setData(category.getListMaterical());
                }
                break;
        }
        ft.commit();

    }

    private void hideAll(FragmentTransaction ft) {
        if (classifyFragment != null) {
            ft.hide(classifyFragment);
        }
        if (listFragment != null) {
            ft.hide(listFragment);
        }
    }

    @Override
    public void setCate(Category cate) {
        dissProgressDialog();
        if (cate.getListCategory() != null) {
            setCurrent(0, cate);
        } else if (cate.getListMaterical() != null) {
            setCurrent(1, cate);
        }
        if (level == 2) {
            //如果都为空 则显示全部的新增
            if ((cate.getListCategory() == null && cate.getListMaterical() == null)) {
                morePopWindow.add_category.setVisibility(View.VISIBLE);
                morePopWindow.matilr_stroage.setVisibility(View.VISIBLE);
                if ((("半成品").equals(parentName) && categoryParent.getIsLocked())
                        || ("外购成品".equals(parentName) && categoryParent.getIsLocked())) {
                    //如果为则不能新增物料
                    morePopWindow.matilr_stroage.setVisibility(View.GONE);
                }
            } else {
                if (cate.getListCategory() != null) {
                    morePopWindow.add_category.setVisibility(View.VISIBLE);
                    morePopWindow.matilr_stroage.setVisibility(View.GONE);

                } else if (cate.getListMaterical() != null) {
                    morePopWindow.add_category.setVisibility(View.GONE);
                    morePopWindow.matilr_stroage.setVisibility(View.VISIBLE);

                    if ((("半成品").equals(parentName) && categoryParent.getIsLocked())
                            || ("外购成品".equals(parentName) && categoryParent.getIsLocked())) {
                        //如果为则不能新增物料
                        morePopWindow.matilr_stroage.setVisibility(View.GONE);
                    }
                }


            }


        } else if (level == 3) {
            if ((cate.getListCategory() == null && cate.getListMaterical() == null)) {
                morePopWindow.add_category.setVisibility(View.VISIBLE);
                morePopWindow.matilr_stroage.setVisibility(View.VISIBLE);
                if ("待分类".equals(category.getName()) && category.getIsLocked()) {
                    morePopWindow.add_category.setVisibility(View.GONE);
                    if ((("半成品").equals(parentName) && categoryParent.getIsLocked())
                            || ("外购成品".equals(parentName) && categoryParent.getIsLocked())) {
                        btnMore.setVisibility(View.GONE);
                    }
                }

            } else {
                if (cate.getListCategory() != null) {
                    morePopWindow.add_category.setVisibility(View.VISIBLE);
                    morePopWindow.matilr_stroage.setVisibility(View.GONE);

                } else if (cate.getListMaterical() != null) {
                    morePopWindow.add_category.setVisibility(View.GONE);
                    morePopWindow.matilr_stroage.setVisibility(View.VISIBLE);

                    if ((("半成品").equals(parentName) && categoryParent.getIsLocked())
                            || ("外购成品".equals(parentName) && categoryParent.getIsLocked())) {
                        //如果为则不能新增物料
                        btnMore.setVisibility(View.GONE);
                    }
                }
            }

        } else if (level == 4) {
            morePopWindow.add_category.setVisibility(View.GONE);
            morePopWindow.matilr_stroage.setVisibility(View.VISIBLE);
            if ((("半成品").equals(parentName) && categoryParent.getIsLocked())
                    || ("外购成品".equals(parentName) && categoryParent.getIsLocked())) {
                //如果为则不能新增物料
                morePopWindow.matilr_stroage.setVisibility(View.GONE);
            }
        }


    }

    @Override
    public void setRes(int i, int res, SweetAlertDialog sweetAlertDialog) {

    }

    @Override
    public void setAddRes(int res, AlertDialog viewDialog) {
        dissProgressDialog();
        if (res==1){
            toast(getString(R.string.up_success));
            viewDialog.dismiss();
            addData();
        }else {
            toast(getString(R.string.up_failure));
        }
    }

    @Override
    public void setDelRes(int res, int pos, SweetAlertDialog sweetAlertDialog) {

    }

    @OnClick(R.id.btn_more)
    public void onViewClicked() {
        morePopWindow.showPopupWindow(btnMore);
    }

    @Override
    public void popWinOnClick(View view) {

        switch (view.getId()) {
            case R.id.add_category:
                View viewDialog = View.inflate(context, R.layout.dialog_edit, null);
                final MaterialEditText myEdit = (MaterialEditText) viewDialog.findViewById(R.id.my_edit);
                final AlertDialog alertDialog = getDialogBuilder(viewDialog, "新增分类", "确定", "取消");
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                            toast("分类不能为空");
                        } else {
                            showProgressDialog();
                            mPresenter.addMaterialCategory(level, myEdit.getText().toString().trim(), user.getOid(),
                                    category.getId(), alertDialog);
                        }
                    }
                });
                break;
            case R.id.matilr_stroage:
                bundle.putInt(Constant.FLAG, 0);
                bundle.putSerializable(Constant.CATEGORY, category);
                intent.setClass(context, MaterialsActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                morePopWindow.dismiss();
                break;
        }

    }

    @Override
    public void readyed() {
        addData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE) {
            addData();
        }
    }

}
